package cn.lopr.im.android.common.app;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author shuangyang7
 * @date 2020/12/30 13:05
 */
public abstract class Fragment extends androidx.fragment.app.Fragment {

    protected View mRootView;
    private Unbinder mRootUnbinder;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //初始化参数
        initArgs(getArguments());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (mRootView == null) {
            int layoutId = getContentLayoutId();
            mRootView = inflater.inflate(layoutId, container, false);
            initWidget(mRootView, inflater);
        } else {
            if (mRootView.getParent() != null) {
                ((ViewGroup) mRootView.getParent()).removeView(mRootView);
            }
        }


        return mRootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initArgs(Bundle arguments) {

    }

    /**
     * 得到当前的资源ID
     *
     * @return 资源文件的ID
     */
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     */
    protected void initWidget(View root, LayoutInflater inflater) {
        mRootUnbinder = ButterKnife.bind(this, root);
    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 返回键点击后调用
     *
     * @return 返回true代表我已经处理, Activity不用关闭
     * 返回false带便没有处理，Activity自己处理
     */
    public boolean onBackPressed() {
        return true;
    }
}
